package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIFillComplaintModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIFillComplaintView;
import vcp.client.forms.ComplaintForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Complaint;
import vcp.entity.Customer;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         complaint controller
 *         </p>
 * 
 * @see UIFillComplaintView
 * @see UIFillComplaintModel
 */
public class UIFillComplaintController extends UIAbstractController implements IFormSubmitionListener <ComplaintForm>
{
	/** reference to the view **/
	private final UIFillComplaintView view;
	/** reference to the model **/
	private final UIFillComplaintModel model;

	/**
	 * 
	 * @param view
	 *            new screen type
	 * @param model
	 *            fill with data
	 */
	public UIFillComplaintController ( UIFillComplaintView view, UIFillComplaintModel model )
	{
		super(Screen.FILL_COMPLAINT);
		this.view = view;
		this.model = model;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************
	/**
	 * Prepare the complaint form
	 * 
	 * @param complaint
	 *            info
	 */
	private synchronized void prepareForReload ( final Complaint complaint )
	{
		this.view.setOnScreen(false);
		this.view.addComplaintID(complaint.getID());

		if ( complaint.getID().intValue() == this.view.getCurrentComplaintID().intValue() )
		{
			this.parentDelegate.showPopUpMessage(String.format(UserNotification.__VALUE__IS_UPDATED, "Complaints info"), PopUpMessageType.INFORMATION);
		}
		this.reloadGUIWithComplaint(complaint);
		this.view.setOnScreen(true);
	}

	/**
	 * get complaint from the history
	 * 
	 * @param complaint
	 *            info
	 */
	private void reloadGUIWithComplaint ( Complaint complaint )
	{
		this.view.loadComplaint(complaint);
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void flashData ()
	{
		this.view.clearView();
		this.model.clearModel();
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Customer )
		{
			this.flashData();
			Customer customer = (Customer) data;
			this.model.setCustomer(customer);

			if ( this.model.hasCustomerComplaints() )
			{
				this.view.addAllComplaintIDs(this.model.getCustomerComplaints());
				this.reloadGUI();;
			}

			this.model.shouldRegisterForUpdates();
			this.view.setOnScreen(true);
		}
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmitionListener(this);
	}

	@Override
	protected void reloadGUI ()
	{
		int complaintID = this.view.getCurrentComplaintID();
		Complaint complaint = this.model.getComplaint(complaintID);
		this.reloadGUIWithComplaint(complaint);
	}

	// ****************************************************************************************************************************
	// IControllerModelDelegate methods
	// ****************************************************************************************************************************
	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		this.parentDelegate.notifyOnDataUpdate(this.model.getCustomer());
		if ( this.view.isOnScreen() )
		{
			if ( newData instanceof Complaint )
			{
				final Complaint complaint = (Complaint) newData;
				SwingUtilities.invokeLater(new Runnable()
					{
						@Override
						public void run ()
						{
							UIFillComplaintController.this.prepareForReload(complaint);
						}
					});
			}
			else if ( newData instanceof String )
			{
				String notification = (String) newData;
				this.parentDelegate.showPopUpMessage(notification, PopUpMessageType.INFORMATION);
			}
		}
	}

	// ****************************************************************************************************************************
	// IControllerViewDelegate methods
	// ****************************************************************************************************************************
	@Override
	public void onButtonPress ( Object object )
	{
		this.reloadGUI();
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <ComplaintForm> event )
	{
		if ( event.getForm() != null )
		{
			this.model.applyComplaintForm(event.getForm());
		}
		else
		{
			this.parentDelegate.showPopUpMessage(UserNotification.WRONG_DATA, PopUpMessageType.ERROR);
		}
	}

}
